const { request } = require('../../services/request')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tongueShapeActive: null,
    tongueShapeActiveIndex: 0,
    coatingActiveIndex: 0,
    reportData: null,
    bigmodelRes: null,
    detectList: [],
    baseInfo: null,
    id: '',
    dialogId: '',
    answeUserId: '',
    userId: JSON.parse(wx.getStorageSync('userInfo')).userId,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { id } = options
    this.setData({
      id,
    })
    request('POST', '/applet/dialog/get/report', { processId: id })
      .then((res) => {
        let data = res.data
        this.setData({
          baseInfo: data,
        })
        this.setData({
          bigmodelRes: JSON.parse(res.data.bigmodelRes),
          dialogId: res.data.dialogId,
          answeUserId: res.data.answeUserId,
        })
        for (let key in data) {
          if (key === 'detectList') {
            this.setData({
              detectList: res.data[key],
            })
          }
          if (key === 'report') {
            let reportData = JSON.parse(data[key])
            reportData.tongueColor.key = reportData.tongueColor.key.replace(
              '舌',
              ''
            )
            this.setData({
              reportData: reportData,
            })
          }
        }
      })
      .catch()
  },
  handleTongueShapeChange(e) {
    this.setData({
      tongueShapeActiveIndex: e.target.dataset.index,
    })
  },
  handleCoatingChange(e) {
    this.setData({
      coatingActiveIndex: e.target.dataset.index,
    })
  },
  handleGoOn() {
    var pages = getCurrentPages()
    console.log('pages :>> ', pages)
    if (pages.length < 2) {
      wx.redirectTo({
        url: `/pages/channel/index?id=${this.data.dialogId}&answeUserId=${this.data.answeUserId}&userId=${this.data.userId}`,
      })
    }
    if (pages[pages.length - 2].route == 'pages/channel/index') {
      console.log('report navigate :>> ', pages[pages.length - 2].route)
      wx.navigateBack({ delta: 1 })
    } else {
      console.log('report redirect :>> ', pages[pages.length - 2].route)
      wx.redirectTo({
        url: `/pages/channel/index?id=${this.data.dialogId}&answeUserId=${this.data.answeUserId}&userId=${this.data.userId}`,
      })
    }
  },
  handleGoPhysique() {
    wx.redirectTo({
      url: `/pages/channel/index?id=${this.data.dialogId}&menuId=1&userId=${
        this.data.userId
      }&answeUserId=${this.data.answeUserId}&defaultQues=${encodeURI(
        '我的体质是什么?'
      )}`,
    })
  },
  handleClickPic(e) {
    const { url } = e.currentTarget.dataset
    let that = this
    wx.previewImage({
      urls: [url],
      success: (res) => {
        console.log(res)
      },
      fail: (err) => {
        console.log('err', err)
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
})
